package com.house365.im.client.util;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.DisplayMetrics;

import com.house365.core.R;
import com.house365.core.application.BaseApplication;
import com.house365.core.util.ViewUtil;

/**
 *@Author Rick.Ping
 *@Date 2014-3-11
 *@Comment
 **/
public class SmileyParser {
	private static SmileyParser sInstance;    
    
    public static SmileyParser getInstance(Context context) {    
    	if(sInstance== null){
    		sInstance = new SmileyParser(context);    
    	}
        return sInstance;    
    }    
    
    // 表情图片集合    
    public static final Integer[] sIconIds = { R.drawable.face1,R.drawable.face2,R.drawable.face3,R.drawable.face4,R.drawable.face5,R.drawable.face6,R.drawable.face7,R.drawable.face8
		,R.drawable.face9,R.drawable.face10,R.drawable.face11,R.drawable.face12,R.drawable.face13,R.drawable.face14
		,R.drawable.face15,R.drawable.face16,R.drawable.face17,R.drawable.face18,R.drawable.face19,R.drawable.face20,R.drawable.face21
		,R.drawable.face22,R.drawable.face23,R.drawable.face24,R.drawable.face25,R.drawable.face26,R.drawable.face27
		,R.drawable.face28};    
    private final Context mContext;    
    private final String[] mSmileyTexts;    
    private final Pattern mPattern;    
    private final HashMap<String, Integer> mSmileyToRes;    
    
    private DisplayMetrics dm;
    private SmileyParser(Context context) {    
        mContext = context;    
        mSmileyTexts = mContext.getResources().getStringArray(    
        		R.array.smiley_array);    
        mSmileyToRes = buildSmileyToRes();    
        mPattern = buildPattern();    
        dm = ((BaseApplication)mContext.getApplicationContext()).getMetrics();
    }    
    public  int getSmileyResource(int which) {    
        return sIconIds[which];    
    }    
    
    private HashMap<String, Integer> buildSmileyToRes() {    
        if (sIconIds.length != mSmileyTexts.length) {    
            throw new IllegalStateException("Smiley resource ID/text mismatch");    
        }    
        HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(    
                mSmileyTexts.length);    
        for (int i = 0; i < mSmileyTexts.length; i++) {    
            smileyToRes.put(mSmileyTexts[i], getSmileyResource(i));    
        }    
        return smileyToRes;    
    }    
    
    // 构建正则表达式    
    private Pattern buildPattern() {    
        StringBuilder patternString = new StringBuilder(mSmileyTexts.length * 3);    
        patternString.append('(');    
        for (String s : mSmileyTexts) {    
            patternString.append(Pattern.quote(s));    
            patternString.append('|');    
        }    
        patternString.replace(patternString.length() - 1, patternString    
                .length(), ")");    
        return Pattern.compile(patternString.toString());    
    }    
    
    // 根据文本替换成图片    
    public CharSequence strToSmiley(CharSequence text) {    
        SpannableStringBuilder builder = new SpannableStringBuilder(text);    
        Matcher matcher = mPattern.matcher(text);    
        while (matcher.find()) {    
            int resId = mSmileyToRes.get(matcher.group());    
            Drawable drawable = mContext.getResources().getDrawable(resId);  
//          int dip = ViewUtil.dip2pix(dm, 16);
//            drawable.setBounds(0, 0, (int)(56*3.0/4), (int)(56*3.0/4));
          drawable.setBounds(0, 0,drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
//          drawable.setBounds(0, 0, 25, 25);//这里设置图片的大小    
            ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);    
            builder.setSpan(imageSpan, matcher.start(),    
                    matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
        }    
        return builder;    
    }

	public String[] getmSmileyTexts() {
		return mSmileyTexts;
	}    
    
    
    
}
